//
//  PTReceiveTimePickView.m
//  PlanTech
//
//  Created by ruifeng song on 2018/7/17.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTReceiveTimePickView.h"
#import "SINACalendarUtils.h"

@interface PTReceiveTimePickView()<UIPickerViewDataSource, UIPickerViewDelegate>
/** 1.选中的字符串 */
@property (nonatomic, strong, nullable)NSString *selectedTitle;

//开始时间
@property (nonatomic, strong, nullable)NSString *startHour;
@property (nonatomic, strong, nullable)NSMutableArray *startHourArray;

//结束时间
@property (nonatomic, strong, nullable)NSString *endHour;
@property (nonatomic, strong, nullable)NSMutableArray *endHourArray;

//日期
@property(nonatomic,strong,nullable)NSString *day;
@property(nonatomic,strong,nullable)NSMutableArray *dayArray;

@end

@implementation PTReceiveTimePickView

@dynamic delegate;

- (void)initPickView
{
    [super initPickView];
    
//    _dataArray=[NSMutableArray mutableCopy];
    
    self.pickerView.frame=CGRectMake(10,  54, self.contentView.frame.size.width-20, self.contentView.frame.size.height-80);
    self.diviLabel.frame=CGRectMake(11, self.contentView.frame.size.height/2-12, 5, 51);
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];

    self.startHour = @"";
    self.endHour = @"";

    self.startHourArray = [[NSMutableArray alloc]init];
    self.endHourArray = [[NSMutableArray alloc]init];
    self.dayArray =[[NSMutableArray alloc]init];
    
    //获取当前日期
    //获取当前月份天数
    //加6天
    NSInteger currendDay = [SINACalendarUtils getDay];
//    NSInteger currendDay = 29;
    NSInteger currentMonthDay = [SINACalendarUtils getCurrentMonthForDays];
    NSInteger currentMonth = [SINACalendarUtils getMonth];

    if ((currentMonthDay - currendDay + 1)>=7 ) {
        
        for (NSInteger i = currendDay; i<(currendDay + 7); i++) {

            [self.dayArray addObject:[NSString stringWithFormat:@"%ld月%ld日",currentMonth,i]];
        }
    }
    else
    {
        for (NSInteger i = currendDay; i< (currendDay +(currentMonthDay - currendDay + 1)); i++) {
            
            [self.dayArray addObject:[NSString stringWithFormat:@"%ld月%ld日",currentMonth,i]];
        }
        
        for (NSInteger j = 1; j<=(7 - (currentMonthDay - currendDay + 1));j++ ) {
            
            [self.dayArray addObject:[NSString stringWithFormat:@"%ld月%ld日",(currentMonth + 1),j]];
        }
    }

    for (NSInteger i=6; i<22; i++) {
        [self.startHourArray addObject:[NSString stringWithFormat:@"%ld",(long)i]];
        [self.endHourArray addObject:[NSString stringWithFormat:@"%ld",(long)i]];
    }
}

-(void)setDefalutSelectRowStr:(NSString *)defalutSelectRowStr{
    _defalutSelectRowStr=defalutSelectRowStr;
    
    for (NSInteger i = 0; i<_startHourArray.count; i++) {
        
        if ([_startHourArray[i] isEqualToString:_defalutSelectRowStr]) {
            
            [self.pickerView reloadAllComponents];
            
            [self.pickerView selectRow:i inComponent:1 animated:NO];
            
            if (i < (_startHourArray.count -1)) {
                
                [self.pickerView selectRow:i+1 inComponent:2 animated:NO];
                self.endHour = self.endHourArray[i + 1];
            }
            
            self.day = self.dayArray[0];
            self.startHour = self.startHourArray[i];
        }
    }
}

// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
// 返回每列的行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
       
        return self.dayArray.count;
    }
    else if(component ==1)
    {
        return  self.startHourArray.count;
    }
    else
    {
        return self.endHourArray.count;
    }
    return 0;
}
// 返回pickerView的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 48;
}
// 返回pickerView的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{

    return  self.frame.size.width/3;
    
}
// 返回pickerView 每行的内容
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    NSString *rowString =[NSString stringWithFormat:@"%ld时",row];
//    if (row<10) {
//        rowString =[NSString stringWithFormat:@"%ld时",row];
//    }
    
    if (component ==0) {
        
        self.day = self.dayArray[row];
    }
    else if (component == 1)
    {
        NSString * rowString=self.startHourArray[row];
//        if ([rowString integerValue]<10) {
//            rowString =[NSString stringWithFormat:@"%@时",rowString];
//        }
        
        [self.pickerView selectRow:row inComponent:2 animated:YES];
        //开始 小时
        self.startHour=rowString;
    }
    else
    {
        //结束 小时
        NSString * rowString= self.endHourArray[row];
//        if ([rowString integerValue]<10) {
//            rowString =[NSString stringWithFormat:@"%@",rowString];
//        }
        self.endHour= rowString;
    }
}

// 返回pickerView 每行的view
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor =[UIColor grayColor];
        }
    }
 
    UIView *rowView = [[UIView alloc]init];
    
    UILabel *label = [[UILabel alloc]init];
    label.font=[UIFont systemFontOfSize:18];
    label.textAlignment = 1;
    
    if (component==0) {
    
        rowView.backgroundColor = [UIColor clearColor];
        label.frame=CGRectMake(10, 0, self.frame.size.width/4, 48);
        label.backgroundColor=[UIColor clearColor];
        [label setTextAlignment:NSTextAlignmentCenter];
        
        label.text= [NSString stringWithFormat:@"%@", self.dayArray[row]];
        
    }else if (component==1) {
        rowView.backgroundColor = [UIColor clearColor];
        label.frame=CGRectMake(self.frame.size.width/4/3, 0, self.frame.size.width/4/2, 48);
        label.backgroundColor=[UIColor clearColor];
        [label setTextAlignment:NSTextAlignmentCenter];
        
        NSString * rowString= [NSString stringWithFormat:@"%@时",self.startHourArray[row]] ;
//        if ([rowString integerValue]<10) {
//            rowString =[NSString stringWithFormat:@"%@",rowString];
//        }
        
        label.text=rowString;
    }
    else
    {
        label.frame=CGRectMake(self.frame.size.width/3/4, 0, self.frame.size.width/4/2, 48);
        label.backgroundColor=[UIColor clearColor];
        [label setTextAlignment:NSTextAlignmentCenter];
        
        NSString * rowString=[NSString stringWithFormat:@"%@时",self.endHourArray[row]];
//        if ([rowString integerValue]<10) {
//            rowString =[NSString stringWithFormat:@"%@",rowString];
//        }
        label.text=rowString;
    }
    [rowView addSubview:label];
    
    return rowView;
}

#pragma mark -点击确定按钮
- (void)clickConfirmButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(serviceTimeSelectPickView:day:startHour:endHour:)]) {
        
        if (self.day.length ==0) {
            //设置默认值，都选中第一个
            self.day = self.dayArray[0];
        }
        if (self.startHour.length ==0) {
             self.startHour=self.startHourArray[0];
        }
        if (self.endHour.length ==0) {
             self.endHour = self.endHourArray[0];
        }
    
        [self.delegate serviceTimeSelectPickView:self day:self.day startHour:self.startHour endHour:self.endHour];
    }
    [super clickConfirmButton];
}
-(void)clickCancelButton{
    if (self.delegate && [self.delegate respondsToSelector:@selector(dismissServiceTimeSelectPickView:)]) {
        
         [self.delegate dismissServiceTimeSelectPickView:self];
    }
    [super clickCancelButton];
}

//- (void)setDataArray:(NSMutableArray *)dataArray
//{
////    _dataArray = dataArray;
//    _selectedTitle = dataArray.firstObject;
//    [self.pickerView reloadAllComponents];
//
//}

@end
